package com.scut.edu.wanyou;

import com.scut.edu.wanyou.net.HttpTask;
import com.scut.edu.wanyou.net.WYParams;
import com.scut.edu.wanyou.util.UserKeeper;
import com.scut.edu.wanyou.util.UserToken;
import com.scut.edu.wanyou.util.WYRequestListener;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {

	private EditText username = null;
	private EditText password = null;
	
	private ImageButton login = null;
	
	public static String TAG = "LoginActivity";
	
	private ProgressDialog progressDialog = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		username = (EditText)findViewById(R.id.login_username);
		password = (EditText)findViewById(R.id.login_password);
		
		login = (ImageButton)findViewById(R.id.login);
		login.setOnClickListener(this);
		
		progressDialog = new ProgressDialog(this);
        progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		progressDialog.setMessage("正在登录...");
	}
	
	private boolean isValid()
	{
		String result = "";
		if(username.getText().toString().equals(""))
		{
			result = "请输入用户名";
		}else if(password.getText().toString().equals("")){
			result = "请输入密码";
		}
		if(!result.equals(""))
		{
			Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
	
	private Handler handler = new Handler()
	{
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 0:
				if(progressDialog.isShowing())
				{
					progressDialog.dismiss();
				}
				Bundle bundle = (Bundle)msg.obj;
				if(bundle.getString("flag").equals("ok"))
				{
					Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();
					UserToken token = new UserToken();
					token.setUserid(bundle.getInt("userid"));
					token.setUsername(bundle.getString("username"));
					token.setPassword(bundle.getString("password"));
					UserKeeper.keepUserToken(getApplicationContext(), token);
					Intent intent = new Intent(LoginActivity.this,MainActivity.class);
					startActivity(intent);
					finish();
				}else {
					Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show();
				}
				break;
			case 1:
				if(!progressDialog.isShowing())
				{
					progressDialog.show();
				}
				break;
			default:
				break;
			}
			
		}
		
	};
	
	private WYRequestListener loginListener = new WYRequestListener() {
		
		@Override
		public void onException(Exception e) {
			// TODO Auto-generated method stub
			Log.i(TAG, "onException"+e.getMessage());
		}
		
		@Override
		public void onError(Error e) {
			// TODO Auto-generated method stub
			Log.i(TAG, "onError:"+e.getMessage());
		}
		
		@Override
		public void onComplete(Bundle bundle) {
			// TODO Auto-generated method stub
			handler.obtainMessage(0,bundle).sendToTarget();
		}
		
		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			Log.i(TAG, "onCancel");
		}

		@Override
		public void onStart() {
			// TODO Auto-generated method stub
			handler.obtainMessage(1).sendToTarget();
		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(isValid())
		{
			WYParams params = new WYParams();
			params.setAction("Http://125.216.245.70/wy/");
			params.add("action", "Login");
			params.add("username", username.getText().toString());
			params.add("password", password.getText().toString());
			new HttpTask(loginListener).execute(params);
		}
	}

}
